package la.renzhen.rtpt.admin.boot.starter;

import la.renzhen.rtpt.admin.boot.starter.consul.ConsulSourceAutoConfiguration;
import la.renzhen.rtpt.admin.boot.starter.jdbc.JdbcSourceAutoConfiguration;
import la.renzhen.rtpt.admin.boot.starter.redis.RedisSourceAutoConfiguration;
import la.renzhen.rtpt.admin.controller.AdminAPIController;
import la.renzhen.rtpt.admin.controller.AdminUIController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * <p>
 *
 * @author <a href="mailto:zhouhaichao@2008.sina.com">haiker</a>
 * @version 13/05/2018 9:45 AM
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(CenterServerProperties.class)
@Import({
        ConsulSourceAutoConfiguration.class,
        RedisSourceAutoConfiguration.class,
        JdbcSourceAutoConfiguration.class
})

public class CenterServerAutoConfigruation extends WebMvcConfigurerAdapter {

    @Autowired
    CenterServerProperties config;

    @Bean
    public AdminAPIController adminAPIController() {
        return new AdminAPIController();
    }

    @Bean
    @ConditionalOnProperty(value = "rtpt.admin.disable-web-ui", matchIfMissing = true, havingValue = "false")
    public AdminUIController adminUIController() {
        return new AdminUIController();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        if (config.isDisableWebUi()) {
            return;
        }
        String contextPath = config.getContextPath();
        registry.addViewController(contextPath).setViewName("redirect:" + contextPath + "/ui");
    }
}